"""
    比较多个文件、字符串
"""
import os
import sys
import multiprocessing


class CMPFiles:

    def __init__(self, *args):
        if args:
            if '\r\n' in args[0][0]:
                split_str = '\r\n'
            elif '\r' in args[0][0]:
                split_str = '\r'
            else:
                split_str = '\n'
            self.data_list = [arg.split(split_str) for arg in args[0]]
            print(self.data_list)

    @staticmethod
    def cmp_with_base(base, data, q):
        print('in cmp with base')
        count = 0
        base_count = 0
        resp_list = []
        while 1:
            if base_count >= len(base):
                break_flag = 'base'  # 是base的话，表示base到头了data还没到头，为sum
                break
            if count >= len(data) or data == ['']:
                break_flag = 'data'
                break
            if base[count].strip() == '' and data[count].strip() != '':
                resp_list.append(
                    {'type_minus': True, 'm_data': base[count], 'type_sum': False, 's_data': '', 'num': count + 1})
                base_count += 1

            if data[count].strip() == '' and base[count].strip() != '':
                resp_list.append(
                    {'type_sum': True, 's_data': data[count], 'type_minus': False, 'm_data': '', 'num': count + 1})
                count += 1
            if base[base_count] != data[count]:
                resp_list.append(
                    {'type_sum': True, 's_data': data[count], 'type_minus': True, 'm_data': base[count],
                     'num': count + 1})
                bstrip_num = base_count
                while 1:
                    if base[bstrip_num].strip() != '':
                        break
                    resp_list.append(
                        {'type_minus': True, 'm_data': base[count], 'type_sum': False, 's_data': '', 'num': count + 1})
                    bstrip_num += 1
                strip_num = count
                while 1:
                    if data[strip_num].strip() != '':
                        break
                    resp_list.append(
                        {'type_sum': True, 's_data': data[count], 'type_minus': False, 'm_data': base[count],
                         'num': count + 1})
                    strip_num += 1
                if bstrip_num + 1 >= len(base):
                    break_flag = 'base'
                    break
                if count + 1 >= len(data):
                    break_flag = 'data'
                    break
                if base[bstrip_num + 1] == data[count]:
                    base_count = bstrip_num
                if base[count] == data[strip_num + 1]:
                    count = strip_num
                base_count += 1
                count += 1
            else:
                base_count += 1
                count += 1

        if break_flag == 'base':
            i = count
            while i < len(data):
                resp_list.append(
                    {'type_sum': True, 's_data': data[count], 'type_minus': False, 'm_data': '', 'num': count + 1})
                i += 1

        if break_flag == 'data':
            i = base_count
            while i < len(base):
                resp_list.append(
                    {'type_minus': True, 'm_data': base[count], 'type_sum': False, 's_data': '', 'num': count + 1})
                i += 1
        q.put(resp_list)

    def cmp_func(self):
        """
        比较多个文件或者字符串
        :return:
        """
        print('in cmp func')

        q = multiprocessing.Queue()
        jobs = []
        base_data = self.data_list[0]
        for i in self.data_list[1:]:
            p = multiprocessing.Process(target=self.cmp_with_base, args=(base_data, i, q))
            jobs.append(p)
            p.start()

        for p in jobs:
            p.join()

        return [q.get() for _ in jobs]

    def handle_input_data(self):
        """
        对输入数据进行处理，默认不处理
        :return:
        """
        pass

    def run(self):
        self.handle_input_data()
        print('in run func')
        return self.cmp_func()

    def __call__(self, *args, **kwargs):
        return self.run()


class GETFileContext:

    def get_file_context(self, f_name, q):
        fap = os.path.join(sys.path[0], f_name)  # file absolute path
        with open(fap, 'r') as f:
            resp = f.read()
        q.put(resp)

    def m_get_fc(self, f_names):
        q = multiprocessing.Queue()
        jobs = []
        for f_name in f_names:
            p = multiprocessing.Process(target=self.get_file_context, args=(f_name, q))
            jobs.append(p)
            p.start()

        for p in jobs:
            p.join()

        return [q.get() for _ in jobs]

    def __call__(self, *args, **kwargs):
        return self.m_get_fc(*args)


if __name__ == '__main__':
    f_names = sys.argv[1:]
    fc_list = GETFileContext()(f_names)
    a = CMPFiles(fc_list)()
    print(a)
